{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Mean = [ 5.55111512e-17 -1.11022302e-16 -7.40148683e-17 -7.40148683e-17]\n",
      "Std deviation = [1. 1. 1. 1.]\n",
      "\n",
      "Min max scaled data:\n",
      " [[1.         0.         1.         0.        ]\n",
      " [0.         1.         0.41025641 1.        ]\n",
      " [0.33333333 0.87272727 0.         0.14666667]]\n",
      "\n",
      "L1 normalized data:\n",
      " [[ 0.25210084 -0.12605042  0.16806723 -0.45378151]\n",
      " [ 0.          0.625      -0.046875    0.328125  ]\n",
      " [ 0.0952381   0.31428571 -0.18095238 -0.40952381]]\n",
      "\n",
      "L2 normalized data:\n",
      " [[ 0.45017448 -0.22508724  0.30011632 -0.81031406]\n",
      " [ 0.          0.88345221 -0.06625892  0.46381241]\n",
      " [ 0.17152381  0.56602858 -0.32589524 -0.73755239]]\n",
      "\n",
      "Binarized data:\n",
      " [[1. 0. 1. 0.]\n",
      " [0. 1. 0. 1.]\n",
      " [0. 1. 0. 0.]]\n",
      "\n",
      "New Encoded vector:\n",
      " [[1. 0. 0. 1. 0. 1. 0. 0. 0. 0. 1.]\n",
      " [0. 1. 0. 0. 1. 0. 0. 0. 1. 1. 0.]\n",
      " [0. 0. 1. 0. 1. 0. 1. 0. 0. 0. 1.]\n",
      " [0. 1. 0. 1. 0. 0. 0. 1. 0. 1. 0.]]\n",
      "\n",
      "Encoded vector:\n",
      " [[0. 0. 1. 0. 1. 0. 0. 0. 1. 1. 0.]]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\KZCF\\Anaconda3\\lib\\site-packages\\sklearn\\preprocessing\\_encoders.py:371: FutureWarning: The handling of integer data will change in version 0.22. Currently, the categories are determined based on the range [0, max(values)], while in the future they will be determined based on the unique values.\n",
      "If you want the future behaviour and silence this warning, you can specify \"categories='auto'\".\n",
      "In case you used a LabelEncoder before this OneHotEncoder to convert the categories to integers, then you can now use the OneHotEncoder directly.\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn import preprocessing\n",
    "\n",
    "data = np.array([[ 3, -1.5,  2, -5.4],\n",
    "                 [ 0,  4,  -0.3, 2.1],\n",
    "                 [ 1,  3.3, -1.9, -4.3]])\n",
    "\n",
    "# mean removal 标准化为均值为0方差为1\n",
    "data_standardized = preprocessing.scale(data)\n",
    "print(\"\\nMean =\", data_standardized.mean(axis=0))\n",
    "print(\"Std deviation =\", data_standardized.std(axis=0))\n",
    "\n",
    "# min max scaling\n",
    "data_scaler = preprocessing.MinMaxScaler(feature_range=(0, 1))\n",
    "data_scaled = data_scaler.fit_transform(data)\n",
    "print(\"\\nMin max scaled data:\\n\", data_scaled)\n",
    "\n",
    "# normalization  l1规范化(除以各项绝对值的和)\n",
    "data_normalized = preprocessing.normalize(data, norm='l1')\n",
    "print(\"\\nL1 normalized data:\\n\", data_normalized)\n",
    "\n",
    "# normalization  l2规范化(除以各项平方的和的平方根)\n",
    "data_normalized = preprocessing.normalize(data, norm='l2')\n",
    "print(\"\\nL2 normalized data:\\n\", data_normalized)\n",
    "\n",
    "# binarization  特征二值化(给定阈值，将特征转换为0/1)\n",
    "data_binarized = preprocessing.Binarizer(threshold=1.4).transform(data)\n",
    "print(\"\\nBinarized data:\\n\", data_binarized)\n",
    "\n",
    "# one hot encoding  类别特征编码(按列进行转换)\n",
    "encoder = preprocessing.OneHotEncoder()\n",
    "e_data=np.array([[0, 2, 1, 12], \n",
    "                 [1, 3, 5, 3], \n",
    "                 [2, 3, 2, 12], \n",
    "                 [1, 2, 4, 3]])\n",
    "encoder.fit(e_data)\n",
    "print(\"\\nNew Encoded vector:\\n\",encoder.transform(e_data).toarray())\n",
    "\n",
    "encoded_vector = encoder.transform([[2, 3, 5, 3]]).toarray()\n",
    "print(\"\\nEncoded vector:\\n\", encoded_vector)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Class mapping:\n",
      "audi --> 0\n",
      "bmw --> 1\n",
      "ford --> 2\n",
      "toyota --> 3\n",
      "\n",
      "Labels = ['toyota', 'ford', 'audi']\n",
      "Encoded labels = [3, 2, 0]\n",
      "\n",
      "Encoded labels = [2, 1, 0, 3, 1]\n",
      "Decoded labels = ['ford', 'bmw', 'audi', 'toyota', 'bmw']\n"
     ]
    }
   ],
   "source": [
    "label_encoder = preprocessing.LabelEncoder()\n",
    "input_classes = ['audi', 'ford', 'audi', 'toyota', 'ford', 'bmw']\n",
    "label_encoder.fit(input_classes)\n",
    "\n",
    "# print classes\n",
    "print(\"\\nClass mapping:\")\n",
    "for i, item in enumerate(label_encoder.classes_):\n",
    "    print(item, '-->', i)\n",
    "\n",
    "# transform a set of classes\n",
    "labels = ['toyota', 'ford', 'audi']\n",
    "encoded_labels = label_encoder.transform(labels)\n",
    "print(\"\\nLabels =\", labels) \n",
    "print(\"Encoded labels =\", list(encoded_labels))\n",
    "\n",
    "# inverse transform\n",
    "encoded_labels = [2, 1, 0, 3, 1]\n",
    "decoded_labels = label_encoder.inverse_transform(encoded_labels)\n",
    "print(\"\\nEncoded labels =\", encoded_labels)\n",
    "print(\"Decoded labels =\", list(decoded_labels))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   Integer Feature Categorical Feature\n",
      "0                0               socks\n",
      "1                1                 fox\n",
      "2                2               socks\n",
      "3                1                 box\n",
      "   Integer Feature  Categorical Feature_box  Categorical Feature_fox  \\\n",
      "0                0                        0                        0   \n",
      "1                1                        0                        1   \n",
      "2                2                        0                        0   \n",
      "3                1                        1                        0   \n",
      "\n",
      "   Categorical Feature_socks  \n",
      "0                          1  \n",
      "1                          0  \n",
      "2                          1  \n",
      "3                          0  \n",
      "   Integer Feature_0  Integer Feature_1  Integer Feature_2  \\\n",
      "0                  1                  0                  0   \n",
      "1                  0                  1                  0   \n",
      "2                  0                  0                  1   \n",
      "3                  0                  1                  0   \n",
      "\n",
      "   Categorical Feature_box  Categorical Feature_fox  Categorical Feature_socks  \n",
      "0                        0                        0                          1  \n",
      "1                        0                        1                          0  \n",
      "2                        0                        0                          1  \n",
      "3                        1                        0                          0  \n",
      "[[1. 0. 0. 0. 0. 1.]\n",
      " [0. 1. 0. 0. 1. 0.]\n",
      " [0. 0. 1. 0. 0. 1.]\n",
      " [0. 1. 0. 1. 0. 0.]]\n",
      "   Integer Feature Categorical Feature\n",
      "0                0               socks\n",
      "1                1                 fox\n",
      "2                2               socks\n",
      "3                1                 box\n",
      "  (0, 0)\t1.0\n",
      "  (0, 5)\t1.0\n",
      "  (1, 1)\t1.0\n",
      "  (1, 4)\t1.0\n",
      "  (2, 2)\t1.0\n",
      "  (2, 5)\t1.0\n",
      "  (3, 1)\t1.0\n",
      "  (3, 3)\t1.0\n",
      "[[1. 0. 0. 0. 0. 1.]\n",
      " [0. 1. 0. 0. 1. 0.]\n",
      " [0. 0. 1. 0. 0. 1.]\n",
      " [0. 1. 0. 1. 0. 0.]]\n",
      "  (0, 0)\t1.0\n",
      "  (0, 5)\t1.0\n",
      "  (1, 1)\t1.0\n",
      "  (1, 4)\t1.0\n",
      "  (2, 2)\t1.0\n",
      "  (2, 5)\t1.0\n",
      "  (3, 1)\t1.0\n",
      "  (3, 3)\t1.0\n"
     ]
    }
   ],
   "source": [
    "demo_df = pd.DataFrame({'Integer Feature': [0, 1, 2, 1],\n",
    "                        'Categorical Feature': ['socks', 'fox', 'socks', 'box']})\n",
    "print(demo_df)\n",
    "\n",
    "#get_dummies 自动编码字符串特征，不会改变整数特征\n",
    "print(pd.get_dummies(demo_df))\n",
    "print(pd.get_dummies(demo_df, columns=['Integer Feature', 'Categorical Feature']))  #指定对哪些列进行编码，整数型也可以指定\n",
    "\n",
    "#demo_df['Integer Feature'] = demo_df['Integer Feature'].astype(str)  #将Integer Feature列改为字符串类型\n",
    "\n",
    "\n",
    "# 设置 sparse=False 返回一个 numpy array, 而不是一个 sparse matrix(索引加值表示的稀疏矩阵)\n",
    "ohe = preprocessing.OneHotEncoder(sparse=False)\n",
    "print(ohe.fit_transform(demo_df))\n",
    "\n",
    "#默认sparse=True\n",
    "ohe = preprocessing.OneHotEncoder()\n",
    "print(demo_df)\n",
    "ohe.fit(demo_df)\n",
    "print(ohe.transform(demo_df))  #按索引加值的形式显示矩阵\n",
    "print(ohe.transform(demo_df).toarray())  #转化为矩阵，也可设置sparse=False得到\n",
    "print(ohe.fit_transform(demo_df))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.  0.  1.  0.  0.  1.]\n",
      " [ 1.  2.  3.  4.  6.  9.]\n",
      " [ 1.  4.  5. 16. 20. 25.]]\n",
      "[[ 1.  0.  1.  0.]\n",
      " [ 1.  2.  3.  6.]\n",
      " [ 1.  4.  5. 20.]]\n",
      "[[ 0.  1.  0.]\n",
      " [ 2.  3.  6.]\n",
      " [ 4.  5. 20.]]\n"
     ]
    }
   ],
   "source": [
    "X = np.arange(6).reshape(3, 2)\n",
    "X\n",
    "\n",
    "poly = preprocessing.PolynomialFeatures(degree = 2)\n",
    "print(poly.fit_transform(X))\n",
    "#array([[ 1.,  0.,  1.,  0.,  0.,  1.],\n",
    "#       [ 1.,  2.,  3.,  4.,  6.,  9.],\n",
    "#       [ 1.,  4.,  5., 16., 20., 25.]])\n",
    "# 设置参数interaction_only = True，不包含单个自变量****n(n>1)特征数据\n",
    "poly = preprocessing.PolynomialFeatures(degree = 2, interaction_only = True)\n",
    "print(poly.fit_transform(X))\n",
    "#array([[ 1.,  0.,  1.,  0.],\n",
    "#       [ 1.,  2.,  3.,  6.],\n",
    "#       [ 1.,  4.,  5., 20.]])\n",
    "# 再添加 设置参数include_bias= False，不包含偏差项数据（最左边全为1的一列数据）\n",
    "poly = preprocessing.PolynomialFeatures(degree = 2, interaction_only = True, include_bias=False)\n",
    "print(poly.fit_transform(X))\n",
    "#array([[ 0.,  1.,  0.],\n",
    "#       [ 2.,  3.,  6.],\n",
    "#       [ 4.,  5., 20.]])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
